/**
 * 
 */
package com.java.test.activity.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.java.test.activity.dto.ActivityManageInfo;
import com.java.test.activity.service.ActivityManageService;
import com.java.test.controller.BaseController;
import com.java.test.system.dto.LoginAccountInfo;
import com.java.test.util.HttpErrorCode;
import com.java.test.util.JsonResult;
import com.java.test.util.PageInfo;

/**
 * @author 活动
 *
 */
@RestController
@RequestMapping("/activit")
public class ActivityManageController extends BaseController {

	@Autowired
	public ActivityManageService activityManageService;

	@RequestMapping("/activityList")
	public JsonResult paytypeList(@RequestBody PageInfo<ActivityManageInfo> info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			PageInfo<ActivityManageInfo> list = activityManageService.selectactivityList(info);
			if (list == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 查询单个活动
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/selectactivityInfo")
	public JsonResult selectactivityInfo(@RequestBody ActivityManageInfo infos) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			ActivityManageInfo info = activityManageService.selectactivityInfo(infos);
			if (info == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(info);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 添加活动
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/addpayTypeInfo")
	public JsonResult addpayTypeInfo(@RequestBody ActivityManageInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = activityManageService.insertActivityManageInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "活动列表-添加活动");
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 修改活动
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/updatepayTypeInfo")
	public JsonResult updatepayTypeInfo(@RequestBody ActivityManageInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = activityManageService.updateActivityManageInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "活动列表-修改活动");
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 删除活动
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/deletepayTypeInfo")
	public JsonResult deletepayTypeInfo(@RequestBody List<ActivityManageInfo> info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int ids[] = info.stream().mapToInt(ActivityManageInfo::getId).toArray();
			int i = activityManageService.deleteActivityManageInfo(ids);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "活动列表-删除活动");
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

}
